﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using TeamOPSchoolCup.Domain.DAL;
using TeamOPSchoolCup.Domain.POCO;

namespace TeamOPSchoolCup.Domain.Services
{
  public class SportService
  {
    public static bool AddSport(Sport sport)
    {
      bool s;
      using(EFDbContext db = new EFDbContext()){
        s = AddSport(db, sport);
      }

      return s;
    }

    public static bool AddSport(EFDbContext db, Sport sport)
    {
      db.Sports.Add(sport);

      return (db.SaveChanges() > 0);
    }

    /*
     * true = Alphabetically sorted list
     */
    public static List<Sport> GetSports(bool sort)
    {
      List<Sport> sports;
      using (EFDbContext db = new EFDbContext())
      {
        sports = GetSports(db, sort);
      }

      return sports;
    }

    /*
     * true = Alphabetically sorted list
     */
    public static List<Sport> GetSports(EFDbContext db, bool sort)
    {
      List<Sport> sports = db.Sports.ToList<Sport>();
      if(sort) sports.Sort();

      return sports;
    }

    public static Sport GetSport(int sportId)
    {
      Sport sport;
      using (EFDbContext db = new EFDbContext())
      {
        sport = GetSport(db, sportId);
      }

      return sport;
    }

    public static Sport GetSport(EFDbContext db, int sportId)
    {
      return db.Sports.Find(sportId);
    }

    public static Sport GetSport(string abbr)
    {
      Sport sport;
      using (EFDbContext db = new EFDbContext())
      {
        sport = GetSport(db, abbr);
      }
      return sport;
    }

    public static Sport GetSport(EFDbContext db, string abbr)
    {
      return db.Sports.Single(x => x.Abbreviation == abbr);
    }

    public static bool UpdateSport(Sport sport)
    {
      bool s;
      using (EFDbContext db = new EFDbContext())
      {
        s = UpdateSport(db, sport);
      }

      return s;
    }

    public static bool UpdateSport(EFDbContext db, Sport sport)
    {
      db.Entry(sport).State = EntityState.Modified;

      return (db.SaveChanges() > 0);
    }
  }
}
